/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Алексей
 */

package ru.amse.podolsky.fractalarch;

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class ArrayExtractor {
    private final int[][] array;
    
    public ArrayExtractor(int[][] array){
        this.array = array;
    }
    
    public Compression extractCompressFromArray() throws IOException{
       int imageWidth = this.array[0][2];
       int imageHeight = this.array[0][3];
       int middleColor = this.array[0][1];
       int step = this.array[0][4];
       
       BufferedImage buff = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
       
        for(int i = 0; i < imageWidth; ++i){
           for(int k = 0; k < imageHeight; ++k){
               //buff.setRGB(i, k, (middleColor << 16) + (middleColor << 8) + middleColor);
               buff.setRGB(i, k, (128 << 16) + (128 << 8) + 128);
           }
        }
        return new Compression(buff,toStr(step));
    }
    
    public int extractStepFromArray(){
        return this.array[0][4];
    }
    
    public DomenParams[] extractParamsFromArray(){
       int length =  this.array[0][0] + 1;  
       DomenParams[] out = new DomenParams[length - 1];         
       for(int i = 1; i < length; ++i){
           out[i-1] = new DomenParams();
       }
       
       for(int i = 1; i < length; ++i){
          out[i-1].domenXCoord = this.array[i][0];
          out[i-1].domenYCoord = this.array[i][1];
          out[i-1].sameBlockNumber = this.array[i][2];
          out[i-1].shift = this.array[i][3];
          out[i-1].numberOfConversion = this.array[i][4];
          out[i-1].testDelta = this.array[i][5];
          }
       return out;
    }
    
    private String toStr(int it){
       StringBuilder sb = new StringBuilder();
       String out; 
       while(it != 0){
           sb.append(it % 10);
           it /= 10;
       }
       sb.reverse();
       out = sb.toString();
       return out;
   }
}
